﻿using System.Collections.Generic;
using System.Collections;
using UnityEngine;


class MonsterCollection
{
    Hashtable monsterHash = new Hashtable();

    public bool IsEmpty => monsterHash.Count == 0;

    public int Length {
        get
        {
            int len = 0;
            foreach (Monster monster in monsterHash.Values)
            {
                if (!monster.isDeadState)
                    len++;
            }
            return len;
        }
    }
    

    public bool IsContain(int id)
    {
        if (monsterHash.ContainsKey(id))
        {
            Monster monster = monsterHash[id] as Monster;
            if (!monster.isDeadState)
                return true;
        }
        return false;
    }

    public void Add(Monster monster)
    {
        monsterHash.Add(monster.MonsterId, monster);
    }

    //帧刷新
    public void GameUpdate()
    {
        foreach(Monster monster in monsterHash.Values)
        {
            if(monster.isDeadState && !monster.GameUpdate())
            {
                monster.Recycle();
            }
        }
        
    }

    //根据ID获取怪物
    public Monster GetMonsterById(int id)
    {
        if (monsterHash.ContainsKey(id))
        {
            Monster monster = monsterHash[id] as Monster;
            if (!monster.isDeadState)
                return monster;
        }
            
        return null;
    }

    //获取最近的一个怪物
    public Monster GetNearMonster()
    {
        float mdis = float.MaxValue;
        Monster monster = null;
        ICollection ls = monsterHash.Values;
        foreach(Monster temp in monsterHash.Values)
        {
            if (temp.isDeadState)
                continue;
            float dis = Vector3.Distance(temp.transform.position, Mgr.mainPlayer.transform.position);
            if(dis < mdis)
            {
                mdis = dis;
                monster = temp;
            }
        }
        return monster;
    }

    //获取当前激活的怪物列表
    public List<Monster> GetMonsterList()
    {
        List<Monster> ls = new List<Monster>();
        foreach (Monster temp in monsterHash.Values)
        {
            if (temp.isDeadState)
                continue;
            ls.Add(temp);
        }
        return ls;
    }

    //从缓存池中获取一个怪物
    public Monster GetNewMonster(EnumMonster monsterType)
    {
        foreach(Monster monster in monsterHash.Values)
        {
            if (monster.isDeadState && monster.monsterType == monsterType)
                return monster;
        }
        return null;
    }
    
}

